package com.aaa.service.impl;

import com.aaa.dao.DeptMapper;
import com.aaa.entity.Dept;
import com.aaa.entity.Emp;
import com.aaa.service.DeptService;
import com.aaa.vo.DeptVo;
import com.aaa.vo.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @program: work713
 * @description:
 * @author: 李梦朝
 * @create: 2024-07-15 09:28
 **/
@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    @Override
    public R selectById(Integer deptno) {
        Dept dept = deptMapper.selectById(deptno);
        if (dept != null){
            return new R(200, "查询成功", dept);
        }else {
            return new R(500, "查询失败", null);
        }
    }

    @Override
    public R insert(Dept dept) {
        int i = deptMapper.insert(dept);
        if (i > 0){
            return new R(200, "添加成功", null);
        }else {
            return new R(500, "添加失败", null);
        }
    }

    @Override
    public R update(Dept dept) {
        int i = deptMapper.updateById(dept);
        if (i > 0){
            return new R(200, "修改成功", null);
        }else {
            return new R(500, "修改失败", null);
        }
    }

    @Override
    public R delete(Integer deptno) {
        int i = deptMapper.deleteById(deptno);
        if (i > 0){
            return new R(200, "删除成功", null);
        }else {
            return new R(500, "删除失败", null);
        }
    }

    @Override
    public R selectByPage(DeptVo deptVo) {
        IPage<Emp> page = new Page<>(deptVo.getCurrent(), deptVo.getPageSize());
        QueryWrapper<Dept> wrapper = new QueryWrapper<>();
        //指定查询的列
        if (StringUtils.isNotBlank(deptVo.getDname())) {
            wrapper.like("dname", deptVo.getDname());
        }
        if (deptVo.getDeptno() != null) {
            wrapper.eq("deptno", deptVo.getDeptno());
        }
        IPage<Dept> deptIPage = deptMapper.selectByPage(page, wrapper);

        return new R(200, "查询成功", deptIPage);
    }
}
